package dhy.lyb.change.controller;

import dhy.lyb.change.model.Activity;
import dhy.lyb.change.model.User;
import dhy.lyb.change.service.ActivityService;
import dhy.lyb.change.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/29.
 */
@Controller
@Scope("prototype")
public class ActivityInfoController extends BaseController {

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @Autowired
    @Qualifier("activityService")
    private ActivityService activityService;

    /**
     * 获取用户还没有选择的训练内容条目
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "activity/getUserExtendsActivityByUser")
    @ResponseBody
    public List<Activity> getUserExtendsActivityByUser(@RequestBody User user) {
        //获取训练内容总量
        Integer activity_count = activityService.getActivityCount();
        //判断是否有至少一项训练
        int count = userService.checkUserActivity(user.getId());
        if (count == 0) {
            //训练列表为空
            return activityService.getRecords();
        }
        if (count == activity_count) {
            //训练内容已满
            return null;
        }
        //获取用户关联id
        User u = userService.getUserActivityById(user);
        List<Activity> activities = u.getActivities();
        List<Integer> ids = new ArrayList<Integer>();
        //遍历
        for (int i = 0; i < activities.size(); i++) {
            ids.add(activities.get(i).getId());
        }
        //挑出列表中没有的训练内容
        return activityService.getExtendsActivities(ids);
    }
}
